package state;

import bean.SensorReading;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.common.state.*;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.checkpoint.ListCheckpointed;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.Collections;
import java.util.List;

/**
 * @Description: TODO QQ1667847363
 * @author: xiao kun tai
 * @date:2021/11/4 19:46
 */
public class State2_KeyedState {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);


        DataStream<String> inputStream = env.socketTextStream("192.168.88.106", 7777);

        DataStream<SensorReading> dataStream = inputStream.map(line -> {
            String[] fields = line.split(",");
            return new SensorReading(fields[0], new Long(fields[1]), new Double(fields[2]));
        });


        //定义一个有状态的map操作，统计当前分区数
        SingleOutputStreamOperator<Integer> resultStream = dataStream
                .keyBy("id")
                .map(new MyKeyCountMapper());

        resultStream.print();
        env.execute();
    }

    //自定义RichMapFunction
    public static class MyKeyCountMapper extends RichMapFunction<SensorReading, Integer> {

        private ValueState<Integer> keyCountState;

        private ListState<String> myListState;

        private MapState<String,Double> myMapState;

        private ReducingState<SensorReading> myReducingState;

        @Override
        public void open(Configuration parameters) throws Exception {
            keyCountState = getRuntimeContext()
                    .getState(new ValueStateDescriptor<Integer>("key-count",Integer.class,0));

            myListState=getRuntimeContext()
                    .getListState(new ListStateDescriptor<String>("my-List",String.class));

            myMapState=getRuntimeContext()
                    .getMapState(new MapStateDescriptor<String,Double>("my-map",String.class,Double.class));

//            myReducingState=getRuntimeContext()
//                    .getReducingState(new ReducingStateDescriptor<SensorReading>("my-map",String.class,Double.class));

        }

        @Override
        public Integer map(SensorReading sensorReading) throws Exception {

            //其他状态API调用

            Iterable<String> strings = myListState.get();
            for (String str : strings) {
                System.out.println(str);
            }
            myListState.add("hello");

            //map state
            myMapState.get("1");
            myMapState.put("2",12.3);
            myMapState.remove("2");


//            myReducingState.add(sensorReading);
//            myReducingState.clear();

            Integer count = keyCountState.value();
            count++;
            keyCountState.update(count);
            return count;
        }
    }

}
